package com.drinks.yiniang.ui.activity.search;

import android.content.Intent;
import android.nfc.tech.NfcA;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.search.SearchGoodsAdapter;
import com.drinks.yiniang.adapter.search.SearchGoodsTagAdapter;
import com.drinks.yiniang.common.recycler.decoration.SearchGoodsItemDecoration;
import com.drinks.yiniang.entity.goods.SearchGoodsJson;
import com.drinks.yiniang.io.api.GoodsApiIO;
import com.drinks.yiniang.ui.activity.goods.GoodsDetailsActivity;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.RecyclerUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;

/**
 * 搜索商品(添加tag标签的搜索)
 *
 * @author han
 * @date 2019/12/01
 */
public class SearchGoodsTagActivity extends BaseTranBarActivity implements TextView.OnEditorActionListener,
        BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {

    //cancel
    private TextView tv_sgd_tag_cancel;
    private EditText et_sgd_tag_content;
    private BaseRecyclerView rcv_search_goods_tag;

    private SearchGoodsTagAdapter mGoodsAdapter;

    //关键词
    private String mKeywords;

    private int mPages;

    @Override
    protected void initView() {
        tv_sgd_tag_cancel = findViewById(R.id.tv_sgd_tag_cancel);

        et_sgd_tag_content = findViewById(R.id.et_sgd_tag_content);

        rcv_search_goods_tag = findViewById(R.id.rcv_search_goods_tag);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        initAdapter();
    }

    private void initAdapter() {
        mGoodsAdapter = new SearchGoodsTagAdapter(mContext, new ArrayList<SearchGoodsJson.SearchGoodsList>());
        mGoodsAdapter.setHeaderAndEmpty(true);
        mGoodsAdapter.setCenterEmpty(getLayoutInflater(), rcv_search_goods_tag);
        mGoodsAdapter.bindToRecyclerView(rcv_search_goods_tag);

        rcv_search_goods_tag.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initListener() {
        tv_sgd_tag_cancel.setOnClickListener(this);
        et_sgd_tag_content.setOnEditorActionListener(this);
        mGoodsAdapter.setOnItemClickListener(this);
        mGoodsAdapter.setOnLoadMoreListener(this, rcv_search_goods_tag);
    }

    @Override
    public void widgetClick(View v) {
        if(v.getId() == R.id.tv_sgd_tag_cancel) {
            finish();
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_search_goods_tag;
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            hideKeyboard(this.getCurrentFocus().getWindowToken());
            mKeywords = StringUtils.getEditTextString(et_sgd_tag_content.getText());
            //TODO Search
            doSearchGoods();
        }
        return false;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        SearchGoodsJson.SearchGoodsList data = mGoodsAdapter.getItem(position);

        if (data == null) {
            return;
        }

        Intent intent = new Intent();
        intent.putExtra(Constants.KeyGoodsId, data.getId());
        intent.putExtra(Constants.KeyName, data.getName());
        intent.putExtra(Constants.KeyMoney, StringUtils.getGoodsPrice(data.getApp_price(), data.getActivity_price()));
        setResult(Constants.RESULT_CODE_NORMAL, intent);
        finish();
    }

    @Override
    public void onLoadMoreRequested() {
        GoodsApiIO.getInstance().doGoodsSearch(mPages, mKeywords, new APIRequestCallback<SearchGoodsJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(SearchGoodsJson res) {
                mPages++;

                mGoodsAdapter.addData(res.getData().getData());

                if (CommonUtils.isHasMoreData(res.getData().getData())) {
                    mGoodsAdapter.loadMoreComplete();
                } else {
                    mGoodsAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mGoodsAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    private void doSearchGoods() {
        if (StringUtils.isEmpty(mKeywords)) {
            return;
        }

        showLoadingDialog();

        mPages = 1;

        GoodsApiIO.getInstance().doGoodsSearch(mPages, mKeywords, new APIRequestCallback<SearchGoodsJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(SearchGoodsJson res) {
                mPages++;

                mGoodsAdapter.getData().clear();
                mGoodsAdapter.addData(res.getData().getData());

                if (CommonUtils.isHasMoreData(res.getData().getData())) {
                    mGoodsAdapter.loadMoreComplete();
                } else {
                    mGoodsAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mGoodsAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
